package org.springblade.modules.admin.controller;

import com.aliyun.oss.ServiceException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import liquibase.pro.packaged.A;
import liquibase.pro.packaged.S;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.admin.dto.BuildingElevatorDTO;
import org.springblade.modules.admin.dto.RobotBuildingElevatorDelDTO;
import org.springblade.modules.admin.dto.RobotBuildingElevatorListDTO;
import org.springblade.modules.admin.entity.ChatGroup;
import org.springblade.modules.admin.entity.ChatGroupBuilding;
import org.springblade.modules.admin.entity.RobotBuildingElevator;
import org.springblade.modules.admin.service.IBuildingElevatorVisitorService;
import org.springblade.modules.admin.service.IChatGroupBuildingService;
import org.springblade.modules.admin.service.IChatGroupService;
import org.springblade.modules.admin.service.IRobotBuildingElevatorService;
import org.springblade.modules.admin.vo.BuildingAlreadyVO;
import org.springblade.modules.admin.vo.BuildingElevatorVO;
import org.springblade.modules.admin.vo.RobotBuildingElevatorTitleVO;
import org.springblade.modules.admin.vo.RobotBuildingElevatorVO;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 当前版本前提：机器人已绑定租户-租户下管理机器人已绑定群-群已绑定项目
 * 机器人推送项目电梯管理控制器
 * @author: Sharry
 * @createTime: 2023/5/18 15:16
 * @version: Version-1.0
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/manage/robot/building/elevator")
@Api(tags = "机器人项目电梯管理",value = "/manage/robot/building/elevator")
public class RobotBuildingElevatorController {

	private final IBuildingElevatorVisitorService bevService;

	private final IRobotBuildingElevatorService rbeService;

	private final IChatGroupBuildingService cgbService;

	private final IChatGroupService cgService;

	/**
	 * 单个群下,批量新增项目-电梯
	 * 新增前先进行数据校验，拆分出来在ServiceImpl处理
	 * @return 是否成功
	 */
	@PostMapping("/addBatch")
	@ApiOperation("添加项目电梯")
	@ApiOperationSupport(order = 10)
	public R<Boolean> addBatch(@RequestBody RobotBuildingElevatorListDTO relDTO){
		// 参数校验
		Long chatgroupId = relDTO.getChatgroupId();
		List<BuildingElevatorVO> buildingElevatorList = relDTO.getBuildingElevatorList();
		if(Func.isNull(chatgroupId) || Func.isEmpty(buildingElevatorList)){
			throw new ServiceException("添加失败！参数不完整！");
		}
		// 获取目标DTO
		List<BuildingElevatorDTO> buildingElevatorDTOList = bevService.getBuildingElevatorSelected(buildingElevatorList);
		return R.data(rbeService.addBatch(chatgroupId,buildingElevatorDTOList));
	}

	/**
	 * 根据 项目 id
	 * 批量删除项目-电梯
	 * @return 是否成功
	 */
	@PostMapping("/del/batch/buildings")
	@ApiOperation("根据项目id批量删除项目电梯")
	@ApiOperationSupport(order = 20)
	public R<Boolean> delByBuildings(@RequestBody RobotBuildingElevatorDelDTO redDTO){
		List<Long> buildingIds = redDTO.getBuildingIds();
		Long chatgroupId = redDTO.getChatgroupId();
		if(Func.isEmpty(buildingIds)){
			throw new ServiceException("删除失败！请传入要删除的项目列表!");
		}
		boolean b = rbeService.delByBuildings(chatgroupId,buildingIds);
		return R.data(b);
	}

	/**
	 *  根据 电梯 id
	 * 批量删除项目-电梯
	 * @return 是否成功
	 */
	@PostMapping("/del/batch/elevators")
	@ApiOperation("根据电梯id批量删除项目电梯")
	@ApiOperationSupport(order = 21)
	public R<Boolean> delByElevators(@RequestBody RobotBuildingElevatorDelDTO redDTO){
		List<Long> elevatorIds = redDTO.getElevatorIds();
		Long chatgroupId = redDTO.getChatgroupId();
		if(Func.isEmpty(elevatorIds)){
			throw new ServiceException("删除失败！请传入要删除的项目列表!");
		}
		boolean b = rbeService.delByElevators(chatgroupId,elevatorIds);
		return R.data(b);
	}

	/**
	 * 项目-电梯列表
	 * @return 项目-电梯列表分页
	 */
	@PostMapping("/building/list")
	@ApiOperation("项目-的电梯列表")
	@ApiOperationSupport(order = 30)
	public R<IPage<RobotBuildingElevatorVO>> list(@RequestBody RobotBuildingElevatorListDTO relDto){
		String elevatorAddress = relDto.getElevatorAddress();
		Long buildingId = relDto.getBuildingId();
		Long chatgroupId = relDto.getChatgroupId();
		Query query = relDto.getQuery();
		IPage<RobotBuildingElevatorVO> page = rbeService.getPageByBuildingId(chatgroupId,elevatorAddress,buildingId,Condition.getPage(query));
		return R.data(page);
	}

	/**
	 * 抬头信息
	 * @return 抬头信息
	 */
	@GetMapping("/title/calculate")
	@ApiOperation("页面抬头小统计")
	@ApiOperationSupport(order = 31)
	public R<RobotBuildingElevatorTitleVO> title(Long chatgroupId){
		RobotBuildingElevatorTitleVO titleVO = rbeService.getTitle(chatgroupId);
		return R.data(titleVO);
	}

	/**
	 * 左侧项目列表
	 */
	@GetMapping("/building/list/left")
	@ApiOperation("左侧项目列表")
	@ApiOperationSupport(order = 32)
	public R<List<BuildingAlreadyVO>> left(Long chatgroupId){
		List<BuildingAlreadyVO> result = rbeService.getLeft(chatgroupId);
		result.forEach(r -> r.setDelShow(Boolean.FALSE));
		return R.data(result);
	}


}
